<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ContractTemplate;
use App\Support\Data;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ContractTemplateController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ContractTemplate(), function (Grid $grid) {

            $grid->disablePerPages();

            if (!Admin::user()->can('batch-delete-template')) {
                $grid->disableRowSelector();
                $grid->disableBatchDelete();
            }

            $grid->scrollbarX();
            $grid->withBorder();
            $grid->addTableClass(['table-text-center']);

            $grid->disableViewButton();
            $grid->enableDialogCreate();

            $grid->disableEditButton();
            $grid->showQuickEditButton();

            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('url', '模板文件')->display(function ($url) {
                return '<a href="' . $url . '">' . $this->title . '</a>';
            });
            // $grid->column('remarks');
            $grid->column('created_at')->sortable();
            $grid->column('status')->switch();
            // $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');


            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('template-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }

            // @permissions
            if ((!Admin::user()->can('template-update'))) {
                $grid->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('template-delete')) {
                $grid->disableDeleteButton();
            }

            if (!Admin::user()->can('template-view')) {
                $grid->disableViewButton();
            }


            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('title');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ContractTemplate(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('url');
            $show->field('remarks');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ContractTemplate(), function (Form $form) {

            $form->disableViewButton();
            $form->hidden('status')
                ->customFormat(function ($v) {
                    return $v == 1 ? 1 : 0;
                })
                ->saving(function ($v) {
                    return $v;
                });

            $form->display('id');
            $form->text('title', '模板名称')->required();
            // $form->file('url', '上传模板')->saveFullUrl()->autoUpload()->move('contract/template/' . date('Y/m') . '/' . uniqid())->required()->saving(function ($path) {
            //     return httpToHttps($path);
            // });
            $form->file('url', '上传模板')->disk('public')->autoUpload()->move('contract/template/' . date('Y/m') . '/' . uniqid())->required();
            // $form->text('remarks');->disk('public')
            $form->radio('status')->options([0 => '未启用', 1 => '启用'])->default(1)->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
